<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 标题区域 -->
      <div class="login-title">
        <h2>智能大棚监控系统</h2>
        <p class="subtitle">Intelligence Greenhouse Monitoring</p>
      </div>

      <!-- 登录表单 -->
      <div class="login-form">
        <div class="form-header">
          <h3>账号登录</h3>
          <p>欢迎回来！请输入您的账号信息。</p>
        </div>

        <el-form 
          ref="loginFormRef"
          :model="loginForm"
          :rules="rules"
          size="large"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              clearable
              class="custom-input"
            >
              <template #prefix>
                <el-icon class="input-icon"><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              show-password
              clearable
              class="custom-input"
            >
              <template #prefix>
                <el-icon class="input-icon"><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <div class="remember-forgot">
            <el-checkbox v-model="rememberMe">记住密码</el-checkbox>
            <a href="javascript:;" class="forgot-link">忘记密码？</a>
          </div>

          <el-button
            type="primary"
            :loading="loading"
            class="submit-btn"
            @click="handleLogin"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>

          <div class="register-tip">
            还没有账号？
            <router-link to="/register" class="register-link">
              立即注册
            </router-link>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance } from 'element-plus'
import { login } from '../api/user'
import type { LoginData } from '../api/user'

const router = useRouter()
const loginFormRef = ref<FormInstance>()
const loading = ref(false)
const rememberMe = ref(false)

const loginForm = reactive<LoginData>({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, message: '用户名长度不能小于5位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    const res = await login(loginForm)
    if (res.token) {
      localStorage.setItem('token', res.token)
      ElMessage.success('登录成功')
      try {
        await router.push('/monitor')
      } catch (routerError) {
        console.error('路由跳转失败:', routerError)
        ElMessage.error('页面跳转失败，请刷新重试')
      }
    } else {
      ElMessage.error('登录失败：未获取到token')
    }
  } catch (error) {
    console.error('登录失败:', error)
    if (!error.message?.includes('route navigation')) {
      ElMessage.error('登录失败，请重试')
    }
  } finally {
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
}

.login-box {
  width: 480px;
  position: relative;
  z-index: 1;
}

.login-title {
  text-align: center;
  margin-bottom: 40px;
  color: white;
  
  h2 {
    font-size: 32px;
    margin: 0 0 8px;
    font-weight: 600;
    text-shadow: 0 2px 4px rgba(0,0,0,0.1);
  }
  
  .subtitle {
    font-size: 16px;
    margin: 0;
    opacity: 0.9;
    letter-spacing: 1px;
  }
}

.login-form {
  background: #fff;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.form-header {
  text-align: center;
  margin-bottom: 24px;
  
  h3 {
    font-size: 24px;
    color: #333;
    margin: 0 0 8px;
  }
  
  p {
    color: #999;
    font-size: 14px;
    margin: 0;
  }
}

.custom-input {
  :deep(.el-input__wrapper) {
    background: #ffffff;
    box-shadow: none !important;
    border: none;
    border-radius: 4px;
    padding: 0 12px;
    
    &.is-focus {
      background: #ffffff;
    }
  }
  
  :deep(.el-input__inner) {
    height: 40px;
    color: #333333;
    font-size: 14px;
    background: #ffffff;
    
    &::placeholder {
      color: #909399;
    }
  }

  .input-icon {
    font-size: 16px;
    color: #909399;
  }
}

:deep(.el-form-item) {
  margin-bottom: 24px;
}

:deep(.el-form-item__error) {
  padding-top: 4px;
  font-size: 13px;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 0 20px;
  
  :deep(.el-checkbox__label) {
    color: #606266;
  }
}

.forgot-link {
  color: #606266;
  font-size: 14px;
  text-decoration: none;
  
  &:hover {
    color: #409eff;
  }
}

.submit-btn {
  width: 100%;
  height: 40px;
  font-size: 16px;
  margin-bottom: 16px;
}

.register-tip {
  text-align: center;
  font-size: 14px;
  color: #606266;
  
  .register-link {
    color: #409eff;
    text-decoration: none;
    
    &:hover {
      color: #66b1ff;
    }
  }
}
</style> 